using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;
using SamasDBI;
using SamasDBI.MySQL;
using MySql.Data.MySqlClient;

namespace EQEmulatorGUI
{
    public partial class frmMain : Form
    {
        private string sSQLLoginFile = "SQL.xml";
        private string sMySQLLoginFile = "MySQL.xml";
       public string host = System.Configuration.ConfigurationManager.AppSettings["host"];
       public string uname = System.Configuration.ConfigurationManager.AppSettings["uname"];
       public string upass = System.Configuration.ConfigurationManager.AppSettings["upass"];
       public string dbhost = System.Configuration.ConfigurationManager.AppSettings["dbhost"];
       public string dbuser = System.Configuration.ConfigurationManager.AppSettings["dbuser"];
       public string dbpass = System.Configuration.ConfigurationManager.AppSettings["dbpass"];
        public string dbname = System.Configuration.ConfigurationManager.AppSettings["dbname"];
        public frmMain()
        {
            InitializeComponent();
        }

        private void serverConsoleFunctionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            frmTelnet fTelnet = new frmTelnet();
            fTelnet.MdiParent = this;
            fTelnet.Show();
            Cursor = Cursors.Default;
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            AboutEQEmuGUI fAboutEQEmuGUI = new AboutEQEmuGUI();
            fAboutEQEmuGUI.MdiParent = this;
            fAboutEQEmuGUI.Show();
            Cursor = Cursors.Default;
        }

        private void serverVariablesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            frmVariables fVariables = new frmVariables();
            fVariables.MdiParent = this;
            fVariables.Show();
            Cursor = Cursors.Default;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            MySQLRunner.ConnectionString = "Server=" + dbhost + ";Uid=" + dbuser + ";Pwd=" + dbpass + ";Database=" + dbname + ";";
        }
        
        private void startEQEmuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process EQWorld = new Process();

            EQWorld.StartInfo.FileName = "start.bat";
            EQWorld.StartInfo.Arguments = "";
            //MdiParent = this;
            EQWorld.Start();
        }

        private void stopEQEmuToolStripMenuItem_Click(object sender, EventArgs e)
        {
 KillProcess("world");
            KillProcess("minilogin");
            KillProcess("zone");
            KillProcess("eqlaunch");
}

private void KillProcess(string processName){
Process myproc= new Process();
//Get all instances of proc that are open, attempt to close them.
foreach (Process thisproc in Process.GetProcessesByName(processName)) {
if(!thisproc.CloseMainWindow()){
//If closing is not successful or no desktop window handle, then force termination.
thisproc.Kill();
}
} // next proc
}

        private void petitionManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            frmPetitions fPetitions = new frmPetitions();
            fPetitions.MdiParent = this;
            fPetitions.Show();
            Cursor = Cursors.Default;
        }

        private void serverRulesManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            frmRules fRules = new frmRules();
            fRules.MdiParent = this;
            fRules.Show();
            Cursor = Cursors.Default;
        }

        private void accountManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            frmAccounts fAccounts = new frmAccounts();
            fAccounts.MdiParent = this;
            fAccounts.Show();
            Cursor = Cursors.Default;
        }
        }
    }